package com.growthsee.rider.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.growthsee.common.core.controller.BaseController;
import com.growthsee.common.core.domain.AjaxResult;
import com.growthsee.common.core.page.PageDomain;
import com.growthsee.common.core.page.TableDataInfo;
import com.growthsee.common.utils.SecurityUtils;
import com.growthsee.rider.domain.RiderCourseCategory;
import com.growthsee.rider.domain.dto.RiderCourseCategoryDto;
import com.growthsee.rider.enumeration.StatusEnum;
import com.growthsee.rider.enumeration.YesNoEnum;
import com.growthsee.rider.param.BaseParam;
import com.growthsee.rider.service.RiderCourseCategoryService;
import com.growthsee.rider.utils.BeanUtilsExt;
import com.growthsee.rider.wrapper.CourseCategoryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author csy
 * @since 2021-05-13
 */
@AllArgsConstructor
@RestController
@RequestMapping("/rider/courseCategory")
@Api("课程类型")
public class RiderCourseCategoryController extends BaseController {

    private final RiderCourseCategoryService courseCategoryService;
    @GetMapping
    public TableDataInfo query(PageDomain page, BaseParam baseParam) {
        LambdaQueryWrapper<RiderCourseCategory> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(RiderCourseCategory::getDeleted, YesNoEnum.NO.getValue());
        if(StringUtils.isNotEmpty(baseParam.getKeywords())){
            queryWrapper.like(RiderCourseCategory::getName, baseParam.getKeywords());
        }
        Page page1 = courseCategoryService.page(new Page<>(page.getPageNum(),page.getPageSize()), queryWrapper);

        Page<RiderCourseCategoryDto> riderCourseCategoryDtoPage = BeanUtilsExt.copyPageData(page1, RiderCourseCategoryDto.class);
        CourseCategoryWrapper.builder().dataHandler(riderCourseCategoryDtoPage.getRecords());
        return getDataTable(riderCourseCategoryDtoPage.getRecords(),riderCourseCategoryDtoPage.getTotal());
    }

    @PostMapping
    public AjaxResult insert(@RequestBody RiderCourseCategory courseCategory) {
        String checkResult = CourseCategoryWrapper.builder().check(courseCategory);
        if(StringUtils.isNotEmpty(checkResult))return AjaxResult.error(checkResult);
        courseCategory.setCreateBy(SecurityUtils.getUsername());
        courseCategory.setCreateTime(new Date());
        courseCategory.setDeleted(YesNoEnum.NO.getValue());
        courseCategory.setStatus(StatusEnum.ENABLE.getValue());
        courseCategoryService.save(courseCategory);
        return AjaxResult.success();
    }

    @PutMapping
    public AjaxResult update(@RequestBody RiderCourseCategory courseCategory) {
        String checkResult = CourseCategoryWrapper.builder().check(courseCategory);
        if(StringUtils.isNotEmpty(checkResult))return AjaxResult.error(checkResult);
        courseCategory.setUpdateBy(SecurityUtils.getUsername());
        courseCategory.setUpdateTime(new Date());
        courseCategoryService.updateById(courseCategory);
        return AjaxResult.success();
    }


    @GetMapping("{id}")
    @ApiOperation(value = "根据id查询题型", notes = "根据id查询题型")
    public AjaxResult findOne(@PathVariable String id) {
        RiderCourseCategory riderCourseCategory = courseCategoryService.getById(id);
        return AjaxResult.success(riderCourseCategory);
    }

    @DeleteMapping("{id}")
    @ApiOperation(value = "根据id查询题型", notes = "根据id查询题型")
    public AjaxResult deleteById(@PathVariable String id) {
        courseCategoryService.update(Wrappers.<RiderCourseCategory>lambdaUpdate()
                .set(RiderCourseCategory::getDeleted,YesNoEnum.YES.getValue())
                .eq(RiderCourseCategory::getId,id));
        return AjaxResult.success();
    }
    @ApiOperation(value = "条件查询题型", notes = "条件查询题型")
    @GetMapping("/param")
    public AjaxResult param(BaseParam baseParam) {
        LambdaQueryWrapper<RiderCourseCategory> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(RiderCourseCategory::getDeleted,YesNoEnum.NO.getValue());
        if(StringUtils.isNotEmpty(baseParam.getKeywords())){
            queryWrapper.like(RiderCourseCategory::getName, baseParam.getKeywords());
        }
        List<RiderCourseCategory> list = courseCategoryService.list(queryWrapper);
        return AjaxResult.success(list);
    }

}

